/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.hydms.dormitoryfiee.web;


import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.hydms.dormitory.entity.DmsDormitory;
import com.thinkgem.jeesite.modules.hydms.dormitory.service.DmsDormitoryService;
import com.thinkgem.jeesite.modules.hydms.dormitoryfiee.entity.DmsDormitoryFee;
import com.thinkgem.jeesite.modules.hydms.dormitoryfiee.service.DmsDormitoryFeeService;
import com.thinkgem.jeesite.modules.hydms.healthcheck.entity.DmsHealthCheck;
import com.thinkgem.jeesite.modules.hydms.illegalregistration.entity.DmsIllegalRegistration;
import com.thinkgem.jeesite.modules.hydms.illegalregistration.service.DmsIllegalRegistrationService;
import com.thinkgem.jeesite.modules.hydms.students.entity.DmsStudents;
import com.thinkgem.jeesite.modules.hydms.students.service.DmsStudentsService;

/**
 * 宿舍费用Controller
 * @author 冉鑫
 * @version 2017-04-05
 */
@Controller
@RequestMapping(value = "${adminPath}/dormitoryfiee/dmsDormitoryFee")
public class DmsDormitoryFeeController extends BaseController {

	
	@Autowired
	private DmsDormitoryService dmsDormitoryService;
	
	@Autowired
	private DmsStudentsService dmsStudentsService;
	
	@Autowired
	private DmsDormitoryFeeService dmsDormitoryFeeService;
	
	@Autowired
	private DmsIllegalRegistrationService dmsIllegalRegistrationService;
	
	
	@ModelAttribute
	public DmsDormitoryFee get(@RequestParam(required=false) String id) {
		DmsDormitoryFee entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = dmsDormitoryFeeService.get(id);
		}
		if (entity == null){
			entity = new DmsDormitoryFee();
		}
		return entity;
	}
	/**
	 * 
	 * out:(学生信息查询)
	 * 
	 * @param dmsStudents
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws ParseException 
	 */
	
	@RequiresPermissions("dormitoryfiee:dmsDormitoryFee:view")
	@RequestMapping(value = "out")
	public String out(DmsStudents dmsStudents,String studentNumber,String dormitoryNumber, HttpServletRequest request, HttpServletResponse response, Model model) throws ParseException {
		System.out.println("学号："+studentNumber);
		System.out.println("宿舍号："+dormitoryNumber);
		
		DmsDormitory dmsDormitory=new DmsDormitory();
		DmsDormitoryFee dmsDormitoryFee=new DmsDormitoryFee();
		DmsIllegalRegistration dmsIllegalRegistration=new DmsIllegalRegistration();
	
		/**获取住宿时间**/
		dmsDormitory.setDormitoryNumber(dormitoryNumber);
		dmsStudents.setStudentNumber(studentNumber);
		
		Date   checkInDate= null;
		Date   checkOutDate=new Date();
		List<DmsStudents> stuList=dmsStudentsService.findList(dmsStudents);
		System.out.println(stuList.size());
		for(DmsStudents stu:stuList){
			checkInDate=stu.getCheckInDate();
		       System.out.println("1:"+checkInDate);
		}
		
		System.out.println(checkInDate);
		/**两个日期之间天数**/
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		
		checkInDate=sdf.parse(sdf.format(checkInDate));
		checkOutDate=sdf.parse(sdf.format(checkOutDate));
	
		Calendar cal = Calendar.getInstance();
		cal.setTime(checkInDate);  
		
	    long time1 = cal.getTimeInMillis(); 
	    cal.setTime(checkOutDate);    
	    long time2 = cal.getTimeInMillis();         
	    int between_days=(int)(time2-time1)/(1000*3600*24);
	    
	    
		System.out.println(between_days+ "");
		
		/**获取住宿费**/
		
		/*String dnumber=dmsStudentsService.get(stuId).getDormitoryNumber();*/
		dmsDormitory.setDormitoryNumber(dormitoryNumber);
	    List<DmsDormitory> dlist=dmsDormitoryService.findList(dmsDormitory);
	    
	    String dorId=null;
	    String roomBed1=null;
	    String fee=null;
	    for(DmsDormitory dmsDormitory1:dlist){
	    	dorId=	dmsDormitory1.getId();
	    	roomBed1=dmsDormitory1.getRoomBed();
	    	fee=dmsDormitory1.getFee();
	    	System.out.println("住宿："+fee);
	    }
		
		int  AaccommodationFee=Integer.parseInt(fee)/30*between_days;//个人住宿费
		
		/**获取水电费用**/
	    int roomBed =Integer.parseInt(roomBed1);
		dmsDormitoryFee.setDormitoryNumber(dormitoryNumber);
		 List<DmsDormitoryFee> flist=dmsDormitoryFeeService.findList(dmsDormitoryFee);
		 
		    String HydropowerFee=null;
		    String otherFee1=null;
		    
		    for(DmsDormitoryFee dmsDormitoryFee1:flist){
		    	HydropowerFee=dmsDormitoryFee1.getHydropowerFee();
		    	otherFee1=dmsDormitoryFee1.getOtherFee();
		    }
		
		double hydropowerFee =between_days*Double.parseDouble(HydropowerFee)/roomBed;//个人总水电费
		  System.out.println("总费用"+hydropowerFee);
		
		/**获取其他费用**/
		dmsDormitoryFee.setDormitoryNumber(dormitoryNumber);
	/*	String sdfg=dmsDormitoryFeeService.get(dmsDormitoryFee).getDormitoryNumber();
		dmsDormitory.setDormitoryNumber(dormitoryNumber);*/
		
		int otherFee=between_days*Integer.parseInt(otherFee1)/roomBed;//个人其他费用.
		System.out.println("其他费用"+otherFee);
	    
		/**个人罚金**/
		dmsIllegalRegistration.setStudentNumber(studentNumber);
		List<DmsIllegalRegistration> list = dmsIllegalRegistrationService.findList(dmsIllegalRegistration);
		double  Illfee=0;//个人罚金
		
		if(list.size()>0){
			for(DmsIllegalRegistration dmsIll : list){
				Illfee+=Double.parseDouble(dmsIll.getFine());
			}
		}
		/**总费用**/
		
		
		double allFee=AaccommodationFee+hydropowerFee+otherFee+Illfee;
	
		dmsStudents.setStudentNumber(studentNumber);
		dmsStudents.setAccommodationFee(AaccommodationFee+"");
		dmsStudents.setHydropowerFee(hydropowerFee+"");
		dmsStudents.setAllFee(allFee+"");
		dmsStudents.setParentsIds(otherFee+"");
		dmsStudents.setFine(Illfee+"");
		dmsStudents.setCheckOutDate(checkOutDate);
		
		dmsDormitoryFeeService.updateFee(dmsStudents);
		List<DmsStudents> pageList = dmsStudentsService.findList(dmsStudents); 
		model.addAttribute("pageList", pageList);
	
		
		return "hydms/dormitoryfiee/dmsDormitoryViews";
	}
	
	

	/**
	 * 查找宿舍水电费信息
	 * @param checkResult
	 * @param request
	 * @param response
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("dormitoryfiee:dmsDormitoryFee:view")
	@RequestMapping(value = "checkResult")
	public void checkResult(DmsDormitoryFee dmsDormitoryFee, HttpServletRequest request,String idDorNum,Date idCheckDate,HttpServletResponse response, Model model) {
		dmsDormitoryFee.setCheckDate(idCheckDate);
		dmsDormitoryFee.setDormitoryNumber(idDorNum);
		List<DmsDormitoryFee> list = dmsDormitoryFeeService.findList(dmsDormitoryFee);
	
		if(list.size()>0){
			try {
				response.getWriter().print("null");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	
	
	
	/**
	 * 
	 * checkOut:(学生退房结算)
	 * 
	 * @param dmsStudents：
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequiresPermissions("dormitoryfiee:dmsDormitoryFee:view")
	@RequestMapping(value = "checkout" )
	public String checkOut(DmsStudents dmsStudents, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<DmsStudents> page = dmsStudentsService.findPage(new Page<DmsStudents>(request, response), dmsStudents); 
		model.addAttribute("page", page);
		return "hydms/dormitoryfiee/dmsDormitoryCheckOut";
	}
	
	
	/**
	 * 
	 * look:(学生退房查看)
	 * 
	 * @param DmsDormitoryFee：
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws ParseException 
	 */
	@RequiresPermissions("dormitoryfiee:dmsDormitoryFee:view")
	@RequestMapping(value = "look" )
	public String look(DmsStudents dmsStudents,String stuId,String dormitoryNumber, HttpServletRequest request, HttpServletResponse response, Model model) throws ParseException {

		List<DmsStudents> pageList = dmsStudentsService.findList(dmsStudents); 
		model.addAttribute("pageList", pageList);
		
		return "hydms/dormitoryfiee/dmsDormitoryViews";
	}
	
	
	@RequiresPermissions("dormitoryfiee:dmsDormitoryFee:view")
	@RequestMapping(value = "list")
	public String list(DmsDormitoryFee dmsDormitoryFee, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<DmsDormitoryFee> page = dmsDormitoryFeeService.findPage(new Page<DmsDormitoryFee>(request, response), dmsDormitoryFee); 
		model.addAttribute("page", page);
		return "hydms/dormitoryfiee/dmsDormitoryFeeList";
	}

	@RequiresPermissions("dormitoryfiee:dmsDormitoryFee:view")
	@RequestMapping(value = "form")
	public String form(DmsDormitoryFee dmsDormitoryFee, Model model) {
		model.addAttribute("dmsDormitoryFee", dmsDormitoryFee);
		return "hydms/dormitoryfiee/dmsDormitoryFeeForm";
	}

	
	@RequiresPermissions("dormitoryfiee:dmsDormitoryFee:edit")
	@RequestMapping(value = "save")
	public String save(DmsDormitoryFee dmsDormitoryFee, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, dmsDormitoryFee)){
			return form(dmsDormitoryFee, model);
		}
		dmsDormitoryFeeService.save(dmsDormitoryFee);
		addMessage(redirectAttributes, "保存宿舍费用成功");
		return "redirect:"+Global.getAdminPath()+"/dormitoryfiee/dmsDormitoryFee/list?repage";
	}
	
	
	@RequiresPermissions("dormitoryfiee:dmsDormitoryFee:edit")
	@RequestMapping(value = "delete")
	public String delete(DmsDormitoryFee dmsDormitoryFee, RedirectAttributes redirectAttributes) {
		dmsDormitoryFeeService.delete(dmsDormitoryFee);
		addMessage(redirectAttributes, "删除宿舍费用成功");
		return "redirect:"+Global.getAdminPath()+"/dormitoryfiee/dmsDormitoryFee/list?repage";
	}

}